<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增选举任务')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-vote-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">内容：</label>
                <div class="col-sm-8">
                    <input id="content" name="content" class="form-control" type="hidden">
                    <div class="summernote"></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">被选举人：</label>
                <div class="col-sm-8">
                    <a class="btn btn-success " onclick="addUserBy();">添加被选举人</a>
                    <input id="userByIds" name="userByIds" type="hidden">
                </div>
            </div>
            <div class="form-group" style="display: none" id="userByName">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <textarea id="userByNames" name="userByNames" class="form-control" type="text" rows="3"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">选举人：</label>
                <div class="col-sm-8">
                    <a class="btn btn-success " onclick="addUser();">添加选举人</a>
                    <input id="userIds" name="userIds" type="hidden">
                </div>
            </div>
            <div class="form-group" style="display: none" id="userName">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <textarea id="userNames" name="userNames" class="form-control" type="text" rows="6"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" >
                </div>
            </div>
            <input name="type" type="hidden" value="2" >
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js"/>
    <script type="text/javascript">
        var prefix = ctx + "vote/vote"
        $("#form-vote-add").validate({
            focusCleanup: true
        });

        $('.summernote').summernote({
            placeholder: '请输入内容',
            height: 240,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }

        /**
         * 被选举人
         */
        function addUserBy() {
            var url = ctx +"vote/vote/userByList";
            $.modal.open("用户列表",url);
        }


        /**
         * 选择人
         */
        function addUser() {
            var url = ctx +"vote/vote/userList";
            $.modal.open("用户列表",url);
        }

        function getNames(userIds) {
            if(userIds == null || userIds ==""){
                $("#userName").hide();
            } else {
                $("#userName").show();
            }
        }

        function getUserByNames(userIds) {
            if(userIds == null || userIds ==""){
                $("#userByName").hide();
            } else {
                $("#userByName").show();
            }
        }

        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#content").val(sHTML);
                $.operate.save(prefix + "/add", $('#form-vote-add').serialize());
            }
        }
    </script>
</body>
</html>